#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2

import logging
import extlib.BeautifulSoup

class RelatedKeyword:
    @staticmethod
    def convert_string(str_value):
        return str_value.encode('utf-8').decode('utf-8')
    
    @staticmethod
    def get(keyword, bRemoveDuplicate=True):
        naver_key = "3ed2479fed5134318514a00d89098ff1"
        base_url = "http://openapi.naver.com/search?target=recmd"
        request_url = base_url + "&key=" + naver_key + "&query=" + keyword
        
        try:
            result = urllib2.urlopen(request_url).read()
            #logging.info("Raw: " + str(result))
        except urllib2.HTTPError, e:
            logging.error("HTTPError RelatedKeyword.get()::" + e.code)
            return None
        except urllib2.URLError, e:
            logging.error("URLError RelatedKeyword.get()::" + e.code)
            return None
            
        retDataSet = []
        soup = extlib.BeautifulSoup.BeautifulSoup(result)
        for item in soup.findAll('item'):
            relatedkey = item.string            
            if bRemoveDuplicate and keyword.upper() in relatedkey.upper():
                continue
            retDataSet.append(relatedkey)
            
        return retDataSet